<!--
 * @Description:
 * @Author: Morgan
 * @Date: 2021-06-24 17:07:01
 * @LastEditors: Morgan
 * @LastEditTime: 2021-07-03 00:02:56
-->

<template>
  <div>
    <div class="btn">
      <el-button class="btn" type="primary" @click="toAdd">添加</el-button>
    </div>
    <!-- {{ privilegeData }} -->
    <!-- 表格开始 -->
    <el-table
      v-loading="loading"
      :data="privilegeData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      stripe
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" label="名称" width="180" />
      <el-table-column prop="route" label="路径" />
      <el-table-column prop="type" label="类型" width="100" />
      <el-table-column prop="num" label="序号" width="100" />
      <el-table-column prop="icon" label="图标" width="100" />
      <el-table-column prop="hidden" label="状态" align="center" width="150">
        <template v-slot="scope">
          <el-tag v-if="scope.row.hidden == 0" type="success" size="mini">显示</el-tag>
          <el-tag v-else type="info" size="mini">隐藏</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-button type="text" size="small" class="red_info" @click="toDelete(scope.row.id)">删除</el-button>
          <el-button type="text" size="small" class="green_info" @click="toEdit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->
    <!-- 模态框开始 -->
    <el-dialog :title="title" :visible.sync="visible" @closed="toClose">
      <!-- {{ form }} -->
      <el-form ref="ruleForm" :model="form" :rules="rules">
        <el-form-item
          label="名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="路径" :label-width="formLabelWidth" prop="route">
          <el-input v-model="form.route" autocomplete="off" />
        </el-form-item>
        <el-form-item label="类型" :label-width="formLabelWidth" prop="type">
          <el-select v-model="form.type" clearable placeholder="请选择">
            <el-option label="菜单" value="menu" />
            <el-option label="方法" value="method" />
          </el-select>
          <el-popover
            placement="top-start"
            title="提示"
            width="200"
            trigger="hover"
            content="菜单控制着用户登录后可以动态显示的菜单树；方法为具体的权限，控制着是否可以调用某些接口"
          >
            <el-button slot="reference" type="text">
              <svg class="icon" width="16px" height="16.00px" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"><path d="M512 993.882353C245.850353 993.882353 30.117647 778.149647 30.117647 512S245.850353 30.117647 512 30.117647s481.882353 215.732706 481.882353 481.882353-215.732706 481.882353-481.882353 481.882353z m0-60.235294c232.869647 0 421.647059-188.777412 421.647059-421.647059S744.869647 90.352941 512 90.352941 90.352941 279.130353 90.352941 512s188.777412 421.647059 421.647059 421.647059z" fill="#000000" /><path d="M459.956706 763.572706c-5.12-34.665412-1.204706-105.893647 0-120.530824l5.300706-165.49647c0.963765-15.36-7.981176-11.444706-14.125177-8.553412-29.816471 14.215529-64.602353 40.357647-64.602353 40.357647s21.172706-57.283765 41.592471-81.227294c15.088941-21.052235 47.465412-49.182118 71.258353-51.410824 28.039529 0.602353 42.405647 4.848941 40.05647 74.842353 0 0-4.698353 215.100235-0.662588 252.596706 4.096 11.956706 43.339294-15.209412 48.850824-19.516235 5.541647-4.306824 56.32-50.176 56.32-50.176S602.352941 759.868235 522.300235 787.787294c-28.521412 9.938824-58.066824 5.150118-62.373647-24.214588z" fill="#000000" opacity=".99" /><path d="M503.717647 285.364706m-52.495059 0a52.495059 52.495059 0 1 0 104.990118 0 52.495059 52.495059 0 1 0-104.990118 0Z" fill="#000000" /><path d="M0 0h1024v1024H0z" fill="#FFF4F4" fill-opacity="0" /></svg>
            </el-button>
          </el-popover>
        </el-form-item>
        <el-form-item label="父权限" label-width="80px">
          <el-cascader
            v-model="form.parentId"
            clearable
            :options="privilegeData"
            :props="{ expandTrigger: 'hover', value: 'id', label: 'name' ,checkStrictly:true}"
          />
        </el-form-item>
        <el-form-item v-if="form.type === 'menu'" label="图标" :label-width="formLabelWidth" prop="icon">
          <el-input v-model="form.icon" autocomplete="off" />
        </el-form-item>
        <el-form-item label="序号" :label-width="formLabelWidth" prop="num">
          <el-input v-model="form.num" autocomplete="off" />
        </el-form-item>
        <el-form-item v-if="form.type === 'menu'" label="状态" :label-width="formLabelWidth" prop="hidden">
          <el-switch
            v-model="form.hidden"
            active-text="隐藏"
            inactive-text="显示"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth">
          <el-input
            v-model="form.description"
            type="textarea"
            :rows="2"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSave">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>

<script>
import { findPrivilegeTree, saveOrUpdate, deleteById } from '@/api/privilege'

export default {
  data() {
    return {
      form: {},
      formLabelWidth: '80px',
      visible: false,
      title: '',
      privilegeData: [],
      rules: {
        name: [{
          required: true,
          message: '请输入权限名称',
          trigger: 'blur'
        }],
        route: [{
          required: true,
          message: '请输入权限路径',
          trigger: 'blur'
        }],
        type: [{
          required: true,
          message: '请选择权限类型',
          trigger: 'blur'
        }]
      }
    }
  },
  computed: {},
  created() {
    this.queryPrivilegeData()
  },
  mounted() {},
  methods: {
    async queryPrivilegeData() {
      this.loading = true
      const res = await findPrivilegeTree()
      this.privilegeData = res.data
      this.loading = false
    },
    toAdd() {
      this.queryPrivilegeData()
      // 清空表单数据
      this.form = {}
      this.title = '添加权限'
      this.visible = true
    },
    toClose() {
      this.$refs.ruleForm.resetFields()
    },
    toSave() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          // console.log('校验通过')
          const res = await saveOrUpdate(this.form)
          // 提示用户保存成功，刷新表格数据，关闭模态框
          this.$notify.success({
            title: '成功',
            message: res.success
          })
          this.queryPrivilegeData()
          // this.toClose();
          this.visible = false
        } else {
          return false
        }
      })
    },
    toEdit(row) {
      this.queryPrivilegeData()
      // 设置表单数据
      this.form = { ...row }
      this.title = '编辑权限'
      this.visible = true
    },
    toDelete(id) {
      this.$confirm('此操作将永久删除该风采, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteById({ id })
        this.$notify.success({
          title: '成功',
          message: res.success
        })
        this.queryPrivilegeData()
      }).catch(() => {
        this.$notify.info({
          title: '取消',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<style scoped>
.btn{
  margin-bottom: 8px;
}
</style>
